package com.baigt.j8.lambda;

import java.util.function.Function;

/**
 * java.util.function.Function
 * Represents a function that accepts one argument and produces a result
 * 代表一个函数(接收一个参数并产生一个结果)
 * 提供了一个函数方法apply
 * Function<T, R>
 *     T 是input 输入参数
 *     R 是返回结果类型
 */
public class _002FunctionDemo {
    public static void main(String[] args) {
        Function<Integer,Integer> function = r -> r*r;
        function.apply(1);

        _002FunctionDemo functionDemo=new _002FunctionDemo();
        System.out.println(functionDemo.function(r -> r * r, 10));
        System.out.println(functionDemo.function(r -> r + r, 10));
        System.out.println(functionDemo.functionCompose( 10,r->r*r,r->r+r));
        System.out.println(functionDemo.functionAndThen( 10,r->r*r,r->r+r));
    }


    public Integer plus(Integer value){
        return value*value;
    }
    public Integer sum(Integer value){
        return value+value;
    }

    public Integer function(Function<Integer,Integer> function,Integer t){
        return function.apply(t);
    }
    public Integer functionCompose(Integer t,Function<Integer,Integer> function,Function<Integer,Integer> compose){
        return function.compose(compose).apply(t);
    }
    public Integer functionAndThen(Integer t,Function<Integer,Integer> function,Function<Integer,Integer> then){
        return function.andThen(then).apply(t);
    }



}
